package com.hjc.config;

import com.hjc.filter.LoginCheckFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 原生servlet配置类
 */
@Configuration
public class ServletConfig {

    //注入redis模板
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>();

    //    创建LoginCheckFilter对象
        LoginCheckFilter loginCheckFilter = new LoginCheckFilter();
    //    给LoginCheckFilter对象注入redis模板
        loginCheckFilter.setRedisTemplate(redisTemplate);
    //    注册LoginCheckFilter
        filterRegistrationBean.setFilter(loginCheckFilter);
    //    配置拦截请求路径:拦截所有请求
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}
